package com.example.lc.mythreedemo;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.xutils.x;

import java.util.List;

/**
 * Created by lc on 2017/4/27.
 */

class MyGridViewAdpter extends BaseAdapter {
    private Context context;// 上下文
    private List<EnterpriseHomeEntity> lists;//数据源
    private int mIndex; // 页数下标，标示第几页，从0开始
    private int mPargerSize;// 每页显示的最大的数量
    private int mod;// 取余
    private int grid_view_num_columns = 4;
    private int iv_width;
    private int iv_height;

    /**
     * 有参构造
     *
     * @param context               上下文
     * @param list                  数据源
     * @param mIndex                页数下标，标示第几页，从0开始
     * @param mPargerSize           每页显示的最大的数量
     * @param grid_view_num_columns GridView列数
     */
    public MyGridViewAdpter(Context context, List<EnterpriseHomeEntity> list,
                            int mIndex, int mPargerSize, int grid_view_num_columns, int width, int height) {
        this.context = context;
        this.lists = list;
        this.mIndex = mIndex;
        this.mPargerSize = mPargerSize;
        this.mod = mIndex * mPargerSize;
        this.grid_view_num_columns = grid_view_num_columns;
        this.iv_width = width;
        this.iv_height = height;
    }

    /**
     * 先判断数据及的大小是否显示满本页lists.size() > (mIndex + 1)*mPagerSize
     * 如果满足，则此页就显示最大数量lists的个数
     * 如果不够显示每页的最大数量，那么剩下几个就显示几个
     */
    @Override
    public int getCount() {
        return lists.size() > (mIndex + 1) * mPargerSize ?
                mPargerSize : (lists.size() - mod);
    }

    @Override
    public EnterpriseHomeEntity getItem(int arg0) {
        return lists.get(arg0 + mod);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0 + mod;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_view, null);
            holder.iv_nul = (ImageView) convertView.findViewById(R.id.item_image);
            holder.tv_text = (TextView) convertView.findViewById(R.id.tv_text);
            holder.rl_content = (RelativeLayout) convertView.findViewById(R.id.rl_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //重新确定position因为拿到的总是数据源，数据源是分页加载到每页的GridView上的
        final int pos = position + mod;//假设mPageSiez

        Graphic.setViewWH(iv_width, iv_height, grid_view_num_columns, 0, holder.iv_nul, new LinearLayout(context));//设置图片宽高
        //假设mPagerSize=8，假如点击的是第二页（即mIndex=1）上的第二个位置item(position=1),那么这个item的实际位置就是pos=9
        x.image().bind(holder.iv_nul, lists.get(pos).getImg_url(), ImageUtils.defaultNameCardOptions());
        holder.rl_content.setVisibility(View.VISIBLE);
        holder.tv_text.setText(lists.get(pos).getTitle());
        return convertView;
    }

    static class ViewHolder {
        private ImageView iv_nul;
        private TextView tv_text;
        private RelativeLayout rl_content;
    }
}
